/******************************init****************************/
function query_course(page) {
	var parm = $('#queryForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('course-query');
	sys_record_parm('course',parm);
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#courseResultList').html(result);
		}
	});
}

function shield_course(code, name) {
	$.Zebra_Dialog('确定是否屏蔽'+ name + ' 课吗？',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
       {caption:'屏蔽',callback:function() {
    	   var url = create_url('course-shield');
    	   aidExecute_course(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}

function unshield_course(code, name) {
	$.Zebra_Dialog('确定要给'+ name + ' 课解除屏蔽吗？',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
       {caption:'解除',callback:function() {
    	   var url = create_url('course-unshield');
    	   aidExecute_course(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}
function aidExecute_course(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				query_course();
			} else {
				sys_msg('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}
/*************edit*****************/

//选择课程
function selectCourse_course(){
	var url = create_url('course-aidInit');
	$.colorbox({overlayClose:false, href:url, data:null, close:'[关闭]', width:'850px', height:'650px', open:true});
}

function queryCourse_course(page) {
	sys_loading();
	var parm = $('#queryAidCourseForm').serialize() + sys_get_page(page);
	var url = create_url('course-aidList');
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#material_courseResultList').html(result);
		}
	});
}
function choiseCourse_course(obj, code, name){
	var row = $("#tjTable").find("tr.footable-even").clone(true);
	row.find("td:eq(0)").html(name).end()
	.find(".cc").attr({"value":code});
	$("#addTbody").append(row);
	obj.parents("tr.footable-even").remove();
	querycodes_course();
}

function querycodes_course(){
	var rows = $("#addTbody").find("tr.footable-even");
	var codes = '';
	rows.each(function(){
		var cc = $(this).find(".cc").val();
		codes = codes + cc + ",";
	});
	$("#codes").val(codes);
}

function confirmCourse_course(){
	var rows = $("#addTbody").find("tr.footable-even");
	/*var sskcs = $("#sskc").find("td.footable-even");
	sskcs.remove();*/
	var aa = $('#itemList');
	var row = $("#sskcTable").find("div.rep");
	rows.each(function(i){
		var cn = $(this).find("td:eq(0)").html();
		var cc = $(this).find(".cc").val();
		var item = row.clone(true);
	    item.find(".title1").html(cn).end()
	     	.find(".cc").attr({"name":"itemList[" + i + "].fc", "value":cc});
	    $('#itemList').append(item);
	});
	$.fn.colorbox.close();
}

function deleteChoiseCourse_course(obj){
	obj.parents("tr.footable-even").remove();
	querycodes_course();
	queryCourse_course();
}

function deletedCourses_course(obj){
	obj.parents("div.rep").remove();
}
function getCodes_course(){
	var rows = $("#itemList").find("div.rep");
	var codes = '';
	if(rows.length > 0){
		console.log("111");
		var row = $("#tjTable").find("tr.footable-even");
		rows.each(function(){
			var item = row.clone(true);
			var cc = $(this).find(".cc").val();
			item.find("td:eq(0)").html($(this).find(".title1").html()).end()
			.find(".cc").val(cc);
			$("#addTbody").append(item);
			codes = cc + ","+codes;
		});
	}
	console.log("2222");
	$("#codes").val(codes);
}

function save_course() {
	var v = $('#name').val();
	if (v == null || v == '') {
		sys_msg('课程名称不能为空');
		return false;
	}
	var parm = $('#editForm').serialize();
	var url = create_url('course-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
		sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功!');
				sys_load_handle('course');
			}else if('exists' == result){
				sys_msg('名称重复!');
			}else{
				sys_msg('操作失败!');
			}
		}
	});
}
function selectLc_course(p){
	var a = '';
	$('div#ptypeObjList').find('input.pc').each(function () {
		if( $(this).val() != '' && $(this).val() != null &&  $(this).val() != undefined){
			a += $(this).val() + ',';
		}
	});
	if (a.length > 0) {
		a = a.substring(0, a.length - 1);
	}
	console.log(p);
	var parm = {'p':p,'a':a};
	var url = create_url('course-aidInit1');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'850px', height:'650px', open:true});
}

function queryLc_course(page) {
	var oldoc = $('#old_oc').val();
	var suc = '';
	$('#selectedListBody').find('.run').each(function () {
		if( $(this).val() != '' && $(this).val() != null &&  $(this).val() != undefined){
			suc += $(this).val() + ',';
		}
	});
	suc += oldoc;
	if (suc.length > 0 && oldoc.length == 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = $('#aidQueryForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('course-aidList1');
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryResultList').html(result);
		}
	});
}


function addLc_course(lc, ln, major) {
	var tr = $('#tempTable').find('.rep').clone(true);
	tr.find('.ln').html(ln).end()
		.find('.major').html(major).end()
		.find('.run').val(lc).end()
		.find('.un').val(ln).end()
	$('#selectedListBody').append(tr);
	queryLc_course($('#aidQueryLcPageTxt').val());
}

function chooseLc_course() {
	var template = $('#tempListDiv').find('.rep');
	var ListTd = $('#ptypeObjList');
	var p = $('#p').val() + 1;
	console.log(p);
	$('#selectedListBody').find('.rep').each(function () {
		var rep = template.clone(true);
		rep.find('.title').html($(this).find('.un').val()).end()
			.find(".pc").attr({"name" : "lcList[" + p + "].lc", "value":$(this).find('.run').val()}).end();
		ListTd.append(rep);
		p++;
	});
	$.fn.colorbox.close();
}
